﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AgendaManager
{
    public static class FormsHelper
    {
        public static frmMDI MDI { get; set; }
        public static bool EsMaximizable { get; set; }


        static List<Form> forms = new List<Form>() 
        {
            //new frmMDI(),
            //new frmLogin(),
            new frmNuevoCaño(),
            new frmBusquedaCaño(),
            new frmNuevoContacto(),
            new frmBusquedaContacto(),
            new frmNuevaUbicacion(),
            new frmSeleccionObra(),
            new frmSeleccionInformeSemanal(),
            new frmNuevaObra(),
            new frmSeleccionObra(),
            new frmUser(),
            new frmContenido(),
            new frmFotos(),
            new frmNuevaEmpresa(),
            new frmNuevaSemana(),
            new frmNuevaUbicacion(),
            new frmNuevoArchivo(),
            new frmNuevoCaño(),
            new frmNuevoInforme(),
            new frmNuevoPais(),
            new frmSeleccionCarpeta(),
            new frmSeleccionEmpresa(),
            new frmSeleccionPais(),
            new frmSeleccionProvincia(),
            new frmSubCarpeta(),
            new frmSemana(),
            new frmPlanos(),
            new frmRecordarPassword(),
            new frmNuevoUsuario(),
            new frmIvoNota()
        };

        // <summary>
        /// Instancia o retorna un formulario del tipo deseado.
        /// </summary>
        /// <typeparam name="T">El tipo de formulario a retornar.</typeparam>
        /// <returns></returns>
        public static T GetForm<T>() where T : Form, new()
        {
            Form ret;
            foreach (Form f in forms)
            {
                if (f is T)
                {
                    ret = f;

                    if (ret == null || ret.IsDisposed)
                        ret = new T();

                    ret.MdiParent = MDI;
                    ret.WindowState = FormWindowState.Normal;

                    return ret as T;
                }
            }

            //Si no lo encontre...
            throw new Exception("Falta agregar el Form al list de Forms en FormsFactory");
        }

        public static T ShowForm<T>() where T : Form, new()
        {
            T f = GetForm<T>();
            PrepararForm(f, MDI);
            return f;
        }

        public static T MostrarForm<T>(T f) where T : Form
        {
            PrepararForm(f, MDI);
            return f;
        }

        public static void PrepararForm(System.Windows.Forms.Form f,
                                System.Windows.Forms.Form MDI)
        {
            //f.MdiParent = MDI;
            //f.BringToFront();
            //f.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            //if (!f.Visible) f.MinimumSize = f.Size;
            ////if (f.Icon == null) f.Icon = MDI.Icon;
            //f.Icon = MDI.Icon;
            
            //f.Height = MDI.Height - 118; //125;
            //f.Width = MDI.Width - ((frmMDI)MDI).barControl.Width - 25; //175; //164; // 175;

            //f.BackColor = System.Drawing.Color.LightBlue; //System.Drawing.Color.LightSteelBlue;
            //f.Show();



            f.MdiParent = MDI;
            f.BringToFront();
            f.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            if (!f.Visible) f.MinimumSize = f.Size;
            //if (f.Icon == null) f.Icon = MDI.Icon;
            f.Icon = MDI.Icon;
            
            //Si es una ventana 'maximizable', entonces no la maximizo, pero si la agrando lo mas posible.
            if (EsMaximizable)
            {
                f.Height = MDI.Height - 118; //125;
                f.Width = MDI.Width - ((frmMDI)MDI).barControl.Width - 25; //175; //164; // 175;
            }

            //ToDo!
            //f.BackColor = System.Drawing.Color.LightBlue; //System.Drawing.Color.LightSteelBlue;
            f.Show();

            //Si es una ventana NO 'maximizable', entonces la vuelvo al estado Normal.
            if (!EsMaximizable)
            {
                f.WindowState = System.Windows.Forms.FormWindowState.Normal;
            }

        }

        public static void ChangeStatusLabel(string msg)
        {
            MDI.lblEstado.Text = msg;
        }

    }
}
